package com.weixue.service.impl;

import com.weixue.common.Constants;
import com.weixue.common.ServiceResultEnum;
import com.weixue.controller.vo.NongFuMallShoppingCartItemVO;
import com.weixue.dao.NongFuMallGoodsMapper;
import com.weixue.dao.NongFuMallShoppingCartItemMapper;
import com.weixue.entity.NongFuMallGoods;
import com.weixue.entity.NongFuMallShoppingCartItem;
import com.weixue.service.NongFuMallShoppingCartService;
import com.weixue.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Day36
 *      购物车业务层逻辑实现类
 */
@Service
public class NongFuMallShoppingCartServiceImpl implements NongFuMallShoppingCartService {

    @Autowired
    private NongFuMallShoppingCartItemMapper nongFuMallShoppingCartItemMapper;

    @Autowired
    private NongFuMallGoodsMapper nongFuMallGoodsMapper;

    /**
     * 1.保存商品至购物车中
     *      根据用户信息和商品信息查询购物项表中是否已存在相同的记录，如果存在则进行修改操作，不存在则进行后续操作
     *      判断商品数据是否正确
     *      判断用户的购物车中的商品数量是否已超出最大限制
     * todo 修改session中购物项数量
     */
    @Override
    public String saveNongFuMallCartItem(NongFuMallShoppingCartItem nongFuMallShoppingCartItem) {
        NongFuMallShoppingCartItem temp = nongFuMallShoppingCartItemMapper.selectByUserIdAndGoodsId(nongFuMallShoppingCartItem.getUserId(), nongFuMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            //todo count = tempCount + 1
            temp.setGoodsCount(nongFuMallShoppingCartItem.getGoodsCount());
            return updateNongFuMallCartItem(temp);
        }
        NongFuMallGoods nongFuMallGoods = nongFuMallGoodsMapper.selectByPrimaryKey(nongFuMallShoppingCartItem.getGoodsId());
        //商品为空
        if (nongFuMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = nongFuMallShoppingCartItemMapper.selectCountByUserId(nongFuMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (nongFuMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (nongFuMallShoppingCartItemMapper.insertSelective(nongFuMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    /**
     * 2.获取我的购物车中的列表数据
     */
    @Override
    public List<NongFuMallShoppingCartItemVO> getMyShoppingCartItems(Long nongFuMallUserId) {
        List<NongFuMallShoppingCartItemVO> nongFuMallShoppingCartItemVOS = new ArrayList<>();
        List<NongFuMallShoppingCartItem> nongFuMallShoppingCartItems = nongFuMallShoppingCartItemMapper.selectByUserId(nongFuMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(nongFuMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> nongFuMallGoodsIds = nongFuMallShoppingCartItems.stream().map(NongFuMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<NongFuMallGoods> nongFuMallGoods = nongFuMallGoodsMapper.selectByPrimaryKeys(nongFuMallGoodsIds);
            Map<Long, NongFuMallGoods> nongFuMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(nongFuMallGoods)) {
                nongFuMallGoodsMap = nongFuMallGoods.stream().collect(Collectors.toMap(NongFuMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (NongFuMallShoppingCartItem nongFuMallShoppingCartItem : nongFuMallShoppingCartItems) {
                NongFuMallShoppingCartItemVO nongFuMallShoppingCartItemVO = new NongFuMallShoppingCartItemVO();
                BeanUtil.copyProperties(nongFuMallShoppingCartItem, nongFuMallShoppingCartItemVO);
                if (nongFuMallGoodsMap.containsKey(nongFuMallShoppingCartItem.getGoodsId())) {
                    NongFuMallGoods nongFuMallGoodsTemp = nongFuMallGoodsMap.get(nongFuMallShoppingCartItem.getGoodsId());
                    nongFuMallShoppingCartItemVO.setGoodsCoverImg(nongFuMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = nongFuMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    nongFuMallShoppingCartItemVO.setGoodsName(goodsName);
                    nongFuMallShoppingCartItemVO.setSellingPrice(nongFuMallGoodsTemp.getSellingPrice());
                    nongFuMallShoppingCartItemVOS.add(nongFuMallShoppingCartItemVO);
                }
            }
        }
        return nongFuMallShoppingCartItemVOS;
    }

    /**
     * 3.修改购物车中的属性
     */
    @Override
    public String updateNongFuMallCartItem(NongFuMallShoppingCartItem nongFuMallShoppingCartItem) {
        NongFuMallShoppingCartItem nongFuMallShoppingCartItemUpdate = nongFuMallShoppingCartItemMapper.selectByPrimaryKey(nongFuMallShoppingCartItem.getCartItemId());
        if (nongFuMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (nongFuMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //todo 数量相同不会进行修改
        //todo userId不同不能修改
        nongFuMallShoppingCartItemUpdate.setGoodsCount(nongFuMallShoppingCartItem.getGoodsCount());
        nongFuMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (nongFuMallShoppingCartItemMapper.updateByPrimaryKeySelective(nongFuMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    /**
     * 4.删除购物车中的商品
     */
    @Override
    public Boolean deleteById(Long nongFuMallShoppingCartItemId) {
        //todo userId不同不能删除
        return nongFuMallShoppingCartItemMapper.deleteByPrimaryKey(nongFuMallShoppingCartItemId) > 0;
    }

    @Override
    public NongFuMallShoppingCartItem getNongFuMallCartItemById(Long nongFuMallShoppingCartItemId) {
        return nongFuMallShoppingCartItemMapper.selectByPrimaryKey(nongFuMallShoppingCartItemId);
    }
}
